<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:str="http://exslt.org/strings"
    xmlns:ep="urn:ep"
    extension-element-prefixes="str">

    <xsl:output method="html" indent="no" />
    <xsl:template match="analysis_history">
        <ol><xsl:apply-templates select="./*" /></ol>
    </xsl:template>

    <xsl:template name="analysis_history_node">
        <xsl:param name="uid" />
        <xsl:choose>
            <xsl:when test="count(./*[name()!='meta' and name()!='savedcluster'])&gt;0"><!-- we have children -->
                <span class="ep_de_node" onclick="ep_toggle_de_node('#node_{$uid}')">
                    <xsl:choose>
                        <!-- TO DO -->
                        <xsl:when test="false() and name()='folder' and not(@is_current='1') and count(ancestor::folder)&gt;0"><img src="{$html_href}/static/images/tree12_closed.png" /></xsl:when>
                        <xsl:otherwise><img src="{$html_href}/static/images/tree12_open.png" /></xsl:otherwise>
                    </xsl:choose>
                </span>
            </xsl:when>
            <xsl:otherwise>
                <span class="ep_de_node" onclick="ep_toggle_de_node('#node_{$uid}')"><img src="{$html_href}/static/images/blank.gif" /></span>
            </xsl:otherwise>
        </xsl:choose>
        <xsl:call-template name="analysis_history_action" />
        <xsl:call-template name="analysis_history_controls" />
        <xsl:call-template name="analysis_history_metadata" />
    </xsl:template>

    <xsl:template name="analysis_history_action">
        <span class="ep_de_item">
            <xsl:choose>
                <xsl:when test="name(..)='analysis_history'">
                    <xsl:choose>
                        <xsl:when test="@data_type='affy'"><img src="{$html_href}/static/images/affy_data.png" /></xsl:when>
                        <xsl:when test="@data_type='arrayexpress'"><img src="{$html_href}/static/images/ae_data.png" /></xsl:when>
                        <xsl:otherwise><img src="{$html_href}/static/images/data.png" /></xsl:otherwise>
                    </xsl:choose>
                </xsl:when>
                <xsl:otherwise><img src="{$html_href}/static/images/action_alt.png" /></xsl:otherwise>
            </xsl:choose>
            <xsl:call-template name="analysis_history_action_title" />
            <span style="color:#666666">
                <xsl:value-of select="concat(' (', @date, ' ', @time, '; ')" />
                <xsl:if test="name(..)!='analysis_history'">
                    <xsl:value-of select="concat(@num_rows, '  rows x ', @num_columns, ' columns')" />
                </xsl:if>
                <xsl:if test="name(..)='analysis_history'">
                    <xsl:value-of select="ep:lookup_species_title_name(@species)"/>
                </xsl:if>)
            </span>
        </span>
    </xsl:template>

    <xsl:template name="analysis_history_action_title">
        <xsl:param name="title" />
        <span id="action_{@id}" style="padding: 1px 3px">
            <xsl:if test="@is_current='1'">
                <xsl:attribute name="class">current</xsl:attribute>
            </xsl:if>
            <a href="javascript:ep_de_change_current_action({@id});"><xsl:value-of select="@epc_name" /></a>
        </span>
    </xsl:template>

    <xsl:template name="analysis_history_controls">
        <span class="ep_de_controls" style="">
            <!--
            <xsl:choose>
                <xsl:when test="name(..)='analysis_history'"><xsl:call-template name="analysis_history_dataset_controls" /></xsl:when>
                <xsl:when test="name()='action'">
            -->
                    <xsl:call-template name="analysis_history_dataset_controls" />
            <!--
                </xsl:when>
            </xsl:choose>
            -->
        </span>
    </xsl:template>

    <xsl:template name="analysis_history_dataset_controls">
        <xsl:if test="@data_type!='affy'">
            <a href="{$cgi_href}/ep_get_data.pl?dataset_id={@dst_dataset_id}" target="_blank"><img src="{$html_href}/static/images/data_view.png" title="View dataset"/></a>
        </xsl:if>
        <a href="{$cgi_href}/ep_manage_annots.pl?action_id={@id}" target="_blank"><img src="{$html_href}/static/images/data_annots.png" title="Manage annotations" /></a>
        <xsl:if test="@data_type!='affy'">
            <a href="{$cgi_href}/ep_manage_factors.pl?action_id={@id}" target="_blank"><img src="{$html_href}/static/images/data_factors.png" title="Manage factors"/></a>
        </xsl:if>
        <a href="{$cgi_href}/display_action_meta.pl?action_id={@id}" target="_blank"><img src="{$html_href}/static/images/action_output_view_alt.png" title="View action output" /></a>
        <!-- TO DO
            <a href="javascript:delete_dataset({parent::node()/@root_dataset_id})"><img src="{$html_href}/static/images/data_delete.png" title="Delete dataset" /></a>
            <input id="chk_dataset_{parent::node()/@root_dataset_id}" name="chk_dataset_{parent::node()/@root_dataset_id}" style="display: none" type="checkbox" />
        -->
    </xsl:template>


    <xsl:template name="analysis_history_action_controls">
        <a href="{$cgi_href}/ep_get_data.pl?dataset_id={@dst_dataset_id}" target="_blank"><img src="{$html_href}/static/images/data_view.png" title="View dataset" /></a>
        <a href="{$cgi_href}/display_action_meta.pl?action_id={@id}" target="_blank"><img src="{$html_href}/static/images/action_output_view_alt.png" title="View action output" /></a>
        <!-- TO DO
            <a href="javascript:saveas_action({@id})"><img src="{$html_href}/static/images/data_save.png" /></a>
            <a href="javascript:delete_action({@action_id})"><img src="{$html_href}/static/images/data_delete.png" /></a>
            <input id="chk_action_{@action_id}" name="chk_action_{@action_id}" style="display: none" type="checkbox" />
        -->
    </xsl:template>

    <xsl:template name="analysis_history_metadata">
        <xsl:if test="name()='action'">
            <span class="ep_de_metadata" id="action_{@id}_histogram"><xsl:value-of select="@histogram" /></span>
            <span class="ep_de_metadata" id="action_{@id}_boxplot"><xsl:value-of select="@lineplot" /></span>
            <span class="ep_de_metadata" id="action_{@id}_legend"><xsl:value-of select="@legend" /></span>
            <span class="ep_de_metadata" id="action_{@id}_cols"><xsl:value-of select="@num_columns" /></span>
            <span class="ep_de_metadata" id="action_{@id}_rows"><xsl:value-of select="@num_rows" /></span>
            <span class="ep_de_metadata" id="action_{@id}_mean"><xsl:value-of select="@mean" /></span>
            <span class="ep_de_metadata" id="action_{@id}_stdev"><xsl:value-of select="@stdev" /></span>
            <span class="ep_de_metadata" id="action_{@id}_selectrows"><xsl:value-of select="concat( $cgi_href, '/ep_select_items.pl?select=rows&amp;dataset_id=', @dst_dataset_id)" /></span>
            <span class="ep_de_metadata" id="action_{@id}_selectcols"><xsl:value-of select="concat( $cgi_href, '/ep_select_items.pl?select=cols&amp;dataset_id=', @dst_dataset_id)" /></span>
            <span class="ep_de_metadata" id="action_{@id}_factors"><xsl:value-of select="@factors" /></span>
        </xsl:if>
    </xsl:template>

    <xsl:template match="meta" />
    <xsl:template match="action">
        <xsl:variable name="uid" select="generate-id(.)" />
        <li id="node_{$uid}">
            <div id="entity_{$uid}" class="unselected" onmouseover="ep_de_select_entity('#entity_{$uid}')" onmouseout="ep_de_unselect_entity('#entity_{$uid}')">
                <xsl:call-template name="analysis_history_node">
                    <xsl:with-param name="uid" select="$uid" />
                </xsl:call-template>
            </div>
            <xsl:if test="count(child::node())&gt;0"><ol><xsl:apply-templates /></ol></xsl:if>
        </li>
    </xsl:template>
</xsl:stylesheet>